<?
echo("
<STYLE type=text/css>
A:link {COLOR: blue;text-decoration:none;}
A:visited {COLOR: blue}
A:hover {COLOR: green}
BODY {COLOR: black} 
</STYLE>
");

session_start(); 
include("lib.php");

define('T_LOG','Log');

// read SESSION parameters ===============================
$g_user     = $_SESSION['user'];
$g_display  = $_SESSION['display'];
$g_time     = $_SESSION['time'];

$bess['intv']     = $_SESSION['intv'];
$bess['graph']    = $_SESSION['graph'];
$bess['height']   = $_SESSION['height'];
$bess['mode']     = $_SESSION['mode'];
$bess['floor']    = $_SESSION['floor'];
$bess['roof']     = $_SESSION['roof'];
$bess['pos']      = $_SESSION['pos'];
$bess['dual']     = $_SESSION['dual'];

$bess['s_year']   = $_SESSION['s_year'];
$bess['s_month']  = $_SESSION['s_month'];
$bess['s_day']    = $_SESSION['s_day'];
$bess['s_hour']   = $_SESSION['s_hour'];
$bess['s_minute'] = $_SESSION['s_minute'];

$bess['t_year']   = $_SESSION['t_year'];
$bess['t_month']  = $_SESSION['t_month'];
$bess['t_day']    = $_SESSION['t_day'];
$bess['t_hour']   = $_SESSION['t_hour'];
$bess['t_minute'] = $_SESSION['t_minute'];

$bess['t_selected'] = $_SESSION['t_selected'];

// GET 
$action = $_GET['a'];
$par    = $_GET['x'];
$ypar   = $_GET['y'];

if($action == 'timesync')
  {
    $bess['s_year'] = '';
  }
if($action == 'graph')
  {
    $bess['graph']   = $par;
    if($ypar)
      {
        if($par=='year')  $bess['s_year']   = $ypar;
        if($par=='month') $bess['s_month']  = $ypar;
        if($par=='day')   $bess['s_day']    = $ypar;
        if($par=='hour')  $bess['s_hour']   = $ypar;
        if($par=='minute')$bess['s_minute'] = $ypar;
        if($par=='detail')$bess['s_day']    = $ypar;
      }
  }
if($action == 'mode')
  {
    $bess['mode']   = $par;
    $bess['floor']  = 'auto';
    $bess['roof']   = 'auto';
    $bess['pos']    = 0;
  }
if($action == 'dual')
  {
    $bess['dual']++;
    if($bess['dual']>1)$bess['dual'] = 0;
  }
if($action == 'height')
  {
    $bess['height']   = $par;
  }
if($action == 'pos')
  {
    $bess['pos'] = $par;
  }

if($action == 'year')
  {
    $bess['s_year']   = $par;
  }
if($action == 'month')
  {
    $bess['s_month']  = $par;
  }
if($action == 'day')
  {
    $bess['s_day']    = $par;
  }
if($action == 'hour')
  {
    $bess['s_hour']   = $par;
  }
if($action == 'minute')
  {
    $bess['s_minute'] = $par;
  }

// POST 
if ($_SERVER['REQUEST_METHOD'] == "POST")
  {
    $action = $_POST['action'];
    if($action == 'login')
      {
    $user = $_POST['user'];
    $pswd = $_POST['pswd'];
    $res = getUser($user,$pswd);
    if($res== B_OK)$g_user = $user; 
      }
    if($action == 'config')
      {
    $user = $_POST['user'];
    //$bess['loglev'] = $_POST['loglev'];
    $bess['implev'] = $_POST['implev'];
        $bess['log'] = $_POST['log'];
    writeUserConfig($user);
      }


    if($action == 'zoom')
      {
        $bess['floor'] = $_POST['floor'];
        $bess['roof']  = $_POST['roof'];
      }

  }


$now = date("Y-m-d H:i:s");
if(!$bess['s_year'])
  {
    $info    = date_parse("$now");    
    $bess['s_year']   = $info["year"]+1;
    $bess['s_month']  = $info["month"]+1;
    $bess['s_day']    = $info["day"]+1;
    $bess['s_hour']   = $info["hour"]+1;
    $bess['s_minute'] = $info["minute"]+1;
    echo("Time Sync<br>");
  }


// write SESSION parameters ===============================
$_SESSION['user']    = $g_user;
$_SESSION['display'] = $g_display;
$_SESSION['time']    = $g_time;

$_SESSION['intv']     = $bess['intv'] ;
$_SESSION['graph']    = $bess['graph'] ;
$_SESSION['height']   = $bess['height'] ;
$_SESSION['mode']     = $bess['mode'] ;
$_SESSION['floor']    = $bess['floor'] ;
$_SESSION['roof']     = $bess['roof'] ;
$_SESSION['pos']      = $bess['pos'] ;
$_SESSION['dual']     = $bess['dual'] ;

$_SESSION['s_year']   = $bess['s_year'] ;
$_SESSION['s_month']  = $bess['s_month'] ;
$_SESSION['s_day']    = $bess['s_day'] ;
$_SESSION['s_hour']   = $bess['s_hour'] ;
$_SESSION['s_minute'] = $bess['s_minute'] ;


$intv     = $bess['intv'] ;
$graph    = $bess['graph'] ;
$height   = $bess['height'];
$mode     = $bess['mode'];
$floor    = $bess['floor'];
if(!isset($floor))$floor = 'auto';
$roof     = $bess['roof'];
if(!isset($roof))$roof = 'auto';
$pos      = $bess['pos'];

$dual     = $bess['dual'];


$s_year   = $bess['s_year'] ;
$s_month  = $bess['s_month'] ;
$s_day    = $bess['s_day'] ;
$s_hour   = $bess['s_hour'] ;
$s_minute = $bess['s_minute'] ;

//========================================================

$user = 1;
for($ii=1;$ii<=14400;$ii++)
  {
    $tick['hour'][$ii] = 0;
    $tick['minute'][$ii] = 0;
    $data[$ii] = 0;
    $freq[$ii] = 0;
  }

$t_year = $s_year - 1;

$t_month = $s_month - 1;
$t_month = addZero($t_month);
//if($t_month < 10)$t_month = '0'.$t_month;

$t_day = $s_day - 1;
$t_day = addZero($t_day);
//if($t_day < 10)$t_day = '0'.$t_day;

$temp = 'bess-'.$user.'-'.$t_year.'-'.$t_month.'-'.$t_day.'.log';
$nn = readLogFile($temp);
echo("<h3>BESS 2013 0.2</h3>");

$date  = date("Y-m-d");	
$pulsDay   = readCurrentSum($user,$date);
$date  = date("Y-m");	
$pulsMonth = readCurrentSum($user,$date);
$date  = date("Y");	
$pulsYear  = readCurrentSum($user,$date);
echo("Today: $pulsDay kWh ($nn)   This month: $pulsMonth kWh   This year: $pulsYear kWh<br>");

$user   = $_GET['user'];
$time   = $_GET['time'];
$puls   = $_GET['puls'];
$power  = $_GET['power'];
$base   = $_GET['base'];
$sigmax = $_GET['sigmax'];

if(!$user)$user = 1;

$year   = $s_year-1;
$month  = $s_month-1;
$day    = $s_day-1;
$hour   = $s_hour-1;
$minute = $s_minute-1;

$maxValue = 0;
$minValue = 9999;

if($graph != 'detail')
  {

    if($s_year)
      {
    for($ii = 2;$ii<=13;$ii++)
      {
        $temp = $logMonth[$user][$s_year][$ii];
        if($graph=='year')
          {
        $data[$ii] = $temp;
        if($maxValue < $temp)$maxValue = $temp;
        if($minValue > $temp)$minValue = $temp;
          }
      } 
      }
    if($graph=='year')
      {
    $data['energy'] = $logYear[$user][$s_year]/1000.; 
    $data['date'] =  $year;
    $data['current'] = $s_year;
      }

    if($s_month)
      {
    for($ii = 2;$ii<=33;$ii++)
      {
        $no = $ii - 1;
        $temp = $logDay[$user][$s_year][$s_month][$ii];
        if($graph=='month')
          {
        $data[$ii] = $temp;
        if($maxValue < $temp)$maxValue = $temp;
        if($minValue > $temp)$minValue = $temp;
          }
      }
      }
    if($graph=='month')
      {
    $data['energy'] = $logMonth[$user][$s_year][$s_month]/1000.; 
    $data['date'] =  $year.'-'.$month;
    $data['current'] = $s_month;
      }


    if($s_day)
      {
    $prev = $s_day - 1;
    if($prev < 1)$prev=$s_day;
    if($prev > 31)$prev =$s_day;
    for($ii = 1;$ii<=24;$ii++)
      {
        $temp  = $logHour[$user][$s_year][$s_month][$s_day][$ii];
        $ctemp = $logHour[$user][$s_year][$s_month][$prev][$ii];
        if($graph=='day')
          {
        $data[$ii]  = $temp;
        $cdata[$ii] = $ctemp;
        if($maxValue < $temp)$maxValue = $temp;
        if($minValue > $temp)$minValue = $temp;
          }
      }
      }
    if($graph=='day')
      {
    $data['energy'] = $logDay[$user][$s_year][$s_month][$s_day]/1000.; 
    $data['date'] =  $year.'-'.$month.'-'.$day;
    $data['current'] = $s_day;
      }


    if($s_hour)
      {
    for($ii = 1;$ii<=60;$ii++)
      {
        $temp = $logMinute[$user][$s_year][$s_month][$s_day][$s_hour][$ii];
        if($graph=='hour')
          {
        $data[$ii] = $temp;
        if($maxValue < $temp)$maxValue = $temp;
        if($minValue > $temp)$minValue = $temp;
          }
      }  
      }
    if($graph=='hour')
      {
    $data['energy'] = $logHour[$user][$s_year][$s_month][$s_day][$s_hour]/1000.; 
    $data['date'] =  $year.'-'.$month.'-'.$day.' hour:'.$hour;
    $data['current'] = $s_hour;
      }


    if($graph=='minute')
      {
    for($ii = 1;$ii<10;$ii++)
      {
        $temp = $logEvent[$user][$s_year][$s_month][$s_day][$s_hour][$s_minute][$ii];
        if($graph=='minute')
          {
        $data[$ii] = $temp;
        if($maxValue < $temp)$maxValue = $temp;
        if($minValue > $temp)$minValue = $temp;
          }
      }  

      }
    if($graph=='minute')
      {
    $data['energy'] = $logMinute[$user][$s_year][$s_month][$s_day][$s_hour][$s_minute]/1000.; 
    $data['date'] =  $year.'-'.$month.'-'.$day.' hour:'.$hour.' minute:'.$minute;
    $data['current'] = $s_minute;
      }

  }

if($graph == 'detail')
  {
    $nn = 0;
    for($ii=1;$ii<=24;$ii++)
      {
    $tick['hour'][$nn] = $ii;
    for($jj=1;$jj<=60;$jj++)
      {
        $tick['minute'][$nn] = $jj;
        for($kk=1;$kk<=20;$kk++)
          {    
        $temp   = $logEvent[$user][$s_year][$s_month][$s_day][$ii][$jj][$kk];
        $temp2  = $logTemp[$user][$s_year][$s_month][$s_day][$ii][$jj][$kk];

        $dayKWH +=$temp;
        if($temp)
          {
            $nn++;
            $data[$nn]  = $temp;
            $data2[$nn] = convTemp($temp2);
            if($maxValue < $temp)$maxValue = $temp;
            if($minValue > $temp)$minValue = $temp;
          }
          }  
      }
      }
    $data['energy'] = $logDay[$user][$s_year][$s_month][$s_day]/1000.;
    $data['date'] =  $year.'-'.$month.'-'.$day;
    $data['current'] = $s_day;
  }
//===========================================================
// Presentation
//===========================================================

echo("<html>");
echo("<head>");
?>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
  function update()
{
  $.get("ajax.php", {user: '1'}, function(data){ $("#getmytime").html(data);});  
  setTimeout('update()', 1000);
}
$(document).ready(function(){update();});
</script>

<?

echo("</head>");
//echo("<body>");
echo("<body onload=\"draw();\">");

echo("<a href=index.php?a=timesync>$now</a><br>");
echo("<div id=\"getmytime\">no data</div>");

//echo("<a href='index.php?a=graph&x=year'>YEAR</a> ");
//echo("<a href='index.php?a=graph&x=month'>MONTH</a> ");
echo("<a href='index.php?a=graph&x=day'>DAY</a> ");
//echo("<a href='index.php?a=graph&x=hour'>HOUR</a> ");
//echo("<a href='index.php?a=graph&x=minute'>MINUTE</a> ");
echo("<a href='index.php?a=graph&x=detail'>DETAIL</a> ");
echo("<a href='index.php?a=mode&x=graph'>GRAPH</a> ");
echo("<a href='index.php?a=mode&x=values'>VALUES</a> ");
echo("<a href='stat.php?user=$user'>STATISTIC</a> ");

echo("<br><a href='index.php?a=height&x=10'>10</a> ");
echo("<a href='index.php?a=height&x=20'>20</a> "); 
echo("<a href='index.php?a=height&x=50'>50</a> "); 
echo("<a href='index.php?a=height&x=100'>100</a> "); 
echo("<a href='index.php?a=height&x=200'>200</a> "); 

if($graph=='detail')
  {
    if($dual)
      echo(" <a href='index.php?a=dual'>DUAL OFF</a> "); 
    else
      echo(" <a href='index.php?a=dual'>DUAL ON</a> ");
  }

echo("<form method=\"post\" action=\"index.php\">");
echo("<input type=\"hidden\" name=\"action\" value=\"zoom\" />");
echo("Lower: <input type=\"text\" size=5 name=\"floor\" value=\"$floor\" />");
echo("Upper: <input type=\"text\" size=5 name=\"roof\" value=\"$roof\" />");
echo("<input type=\"submit\" value=\"submit\" name=\"submit\"><br />");
echo("</form>"); 

$data['max_value'] = $maxValue;
if($floor == 'auto')$floor = $minValue;
if($roof == 'auto')$roof = $maxValue;

if($graph)
  {
    histogram($mode,$graph,$height,$floor,$roof,$pos);
    canHistogram($mode,$graph,$height,$floor,$roof,$pos);
  }

if($dual)
  {

    if($graph == 'detail')
      {
    $prev = $s_day - 1;
    if($prev < 1)$prev=$s_day;
    if($prev > 31)$prev =$s_day;

    $nn = 0;
    for($ii=1;$ii<=24;$ii++)
      {
        $tick['hour'][$nn] = $ii;
        for($jj=1;$jj<=60;$jj++)
          {
        $tick['minute'][$nn] = $jj;
        for($kk=1;$kk<=20;$kk++)
          {    
            $temp  = $logEvent[$user][$s_year][$s_month][$prev][$ii][$jj][$kk];
            $dayKWH +=$temp;
            if($temp)
              {
            $nn++;
            $data[$nn] = $temp;
            if($maxValue < $temp)$maxValue = $temp;
            if($minValue > $temp)$minValue = $temp;
              }
          }  
          }
      }
    $data['energy'] = $logDay[$user][$s_year][$s_month][$prev]/1000.;
    $prev_day = $day - 1;
    $data['date'] =  $year.'-'.$month.'-'.$prev_day;
    $data['current'] = $prev_day;

    histogram($mode,$graph,$height,$floor,$roof,$pos);
    canHistogram($mode,$graph,$height,$floor,$roof,$pos);
      } 

  }


//fft();

echo("</body>");
echo("</html>");
?>
